<?php

/***  DOCUMENTATION LAYER

Ceo Sample Unit Test

Last Update: Aug 2007
  
DESCRIPTION
  this file provides a template for other simple unit tests.
  
NOTES
  This version deprecated -- new noncing model does not work with manual
  attempt_login call below.

  This test uses the ace_test db (db login/pw set below)
  
  $test_root dynamically sets root in order to keep tests working after
  they are moved to the __REPOSITORY directory
    
______________________________________________________________________________*/

ob_start();
$_tx0 = microtime();
$test_root = ( strpos(dirname(__FILE__), 'repository') === false ) ? '' : '../';


// Test Name
$test_name = 'ceo ace test';

// Include Files
require_once($test_root . '../php_ceo/ext/ace/ace.class.php');

// Configure Test
$debug = 1;


// *** TEST

// Load Full Framework

  // call CEO project driver (use dirname or ../ to locate directory)
	$_PATH['project_driver'] = '../www/ceo.project_driver.inc.php';
	require_once($_PATH['project_driver']);
	
  // create framework object and load
  $Ceo = new CeoFramework($debug=0);
  $Ceo->Err->filter_trivial = 1;
  $Ceo->load_framework();
  $Ceo->debug = 0;
  $Ceo->print_d('framework load time: ' . $Ceo->tx_diff($_PAGE['tx1']));
  
  // load extensions
  $Ceo->load_ext('ace');
  
  // include additional files
  
  // Test Ace
  $Ceo->print_d('test ace attempt_login', 'purple');
  $Ceo->debug = 1;
  $Ceo->Ace->debug = 1;
  
  // get user and add to session
  $RAND_USER = array ( 'admin', 'member', 'guest', 'inactive', 'troll', 'does not exist' );
  if ( empty($_SESSION['rand_user']) )
  {
    $_SESSION['rand_user'] = $RAND_USER[array_rand($RAND_USER)];
  }
  
  // set login info
  $uh = $_SESSION['rand_user'];
  $pwpt = $uh;
  $nonce = $Ceo->Ace->get_login_nonce();
  $pw = md5($Ceo->Ace->_hash($pwpt).$nonce);
  $Ceo->print_d("pw [salt]: $pwpt [{$Ceo->Ace->hash_salt}]");
  $Ceo->print_d('dbpw (hashed pw): ' . $Ceo->Ace->_hash($pwpt));
  $Ceo->print_d("<h2>testing with user [$uh]</h2>", 'blue');
  
  // connect to DB
  $Ceo->Ace->Dbo->debug = 1;
  $db = 'ace_test';
  $Ceo->Ace->Dbo->connect($db, 'root', 'root');
  
  if ( $Ceo->Ace->USER['is_logged_in'] )
  {
    $Ceo->print_d("user {$Ceo->Ace->USER['handle']} has been found in db by attempt_login -- attempt_login redirects here now where normally level and status are checked by restrict page method to see if user to be admitted", 'orange');
    $Ceo->print_d(print_r($Ceo->Ace->USER,1), 'blue');
    
    // now to check credentials
    $req_level = 2;
    $Ceo->print_d('now, to manually check credentials with method restrict_access_below.  if high enough, you\'re in.  if not, you\'re not');
    $Ceo->print_d("your status / level : {$Ceo->Ace->USER['status']} / {$Ceo->Ace->USER['level']}", 'darkblue');
    if ( $Ceo->Ace->restrict_access_below($req_level) )
    {
      $Ceo->print_d('you would be admitted to this page -- congrats! here\'s your secret content:', 'green');
      print '<img src="http://img462.imageshack.us/img462/8608/animpwnedup6.gif" alt="secret stuff" />';
    }
    else
    {
      $Ceo->print_d('you would not be admitted to this page -- sorry', 'red');
    }
  }
  elseif ( $Ceo->Ace->attempt_login($uh, $pw) )
  {
    $Ceo->print_d("login successful for [$uh]", 'green');
  }
  else
  {
    $Ceo->print_d("login failed for [$uh]", '#660000');
  }
  
  $Ceo->Ace->_reset_user();
  $_SESSION['rand_user'] = null;


// Footer
$_txn = microtime();
$stx = number_format(((substr($_txn,0,9)) + (substr($_txn,-10)) - (substr($_tx0,0,9)) - (substr($_tx0,-10))),4); 
echo "<h6>test '$test_name' complete in $stx s</h6>";
?>
